﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Model;
using DAL;

namespace BLL
{


    public class RoleManager
    {

        private static EFHelper<OASystemEntities> _efHelper = new EFHelper<OASystemEntities>();

        #region  根据员工ID查询该员工有几个角色
        /// <summary>
        /// 根据员工ID查询该员工有几个角色
        /// </summary>
        /// <param name="userId"></param>
        /// <returns></returns>
        public static List<OA_Role> GetRoleByUserId(int userId)
        {
            try
            {


                List<OA_Role> roleList = new List<OA_Role>();

                var roleUserList = _efHelper.QueryList<OA_UserRoleRelation>(u => u.UserId == userId);

                if (roleUserList.Count != 0)
                    foreach (var item in roleUserList)
                    {
                        //var role = _efHelper.GetInfo<OA_Role>(r => r.Id == item.RoleId);
                        //if (role != null)
                        //    roleList.Add(role);
                        roleList.AddRange(_efHelper.QueryList<OA_Role>(r => r.Id == item.RoleId));
                    }


                return roleList;

            }
            catch (Exception ex)
            {

                return new List<OA_Role>();
            }

        }
        #endregion


        #region  查询角色表集合
        /// <summary>
        /// 查询角色表集合
        /// </summary>
        /// <returns></returns>
        public List<OA_Role> QueryRoleList()
        {
            try
            {
                return _efHelper.QueryList<OA_Role>(null).Distinct<OA_Role>(new RoleCompare()).ToList();

            }
            catch (Exception ex)
            {
                return new List<OA_Role>();
            }
        }
        #endregion



        #region 根据UserId查询返回相应的角色集合

        public static List<OA_UserRoleRelation> QueryListByUserId(int userId)
        {

            try
            {
              List<OA_UserRoleRelation> list =   _efHelper.QueryList<OA_UserRoleRelation>(u => u.UserId == userId);

              if (list.Count != 0)
                  return list;
              else
                  return new List<OA_UserRoleRelation>();

            }
            catch (Exception ex)
            {
                
                throw;
            }

        }


        #endregion


        #region 给指定员工分配指定的角色
        /// <summary>
        /// 给指定员工分配指定的角色
        /// </summary>
        /// <param name="RoleIdStr"></param>
        /// <param name="userId"></param>
        /// <returns></returns>
        public bool Add(string RoleIdStr, int userId)
        {
            try
            {
                if (RoleIdStr == null || RoleIdStr == "")
                    return false;

                //查询该员工是否已经分配过角色
                var list = _efHelper.QueryList<OA_UserRoleRelation>(r => r.UserId == userId);

                if (list.Count != 0)
                {
                    _efHelper.Delete<OA_UserRoleRelation>(list);
                    //foreach (OA_UserRoleRelation item in list)
                    //{
                    //    _efHelper.Delete<OA_UserRoleRelation>(item);
                    //}
                }


                string[] IdArray = RoleIdStr.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                var num = 0;
                if (IdArray.Length > 0)
                {
                    foreach (string roleId in IdArray)
                    {
                        OA_UserRoleRelation relation = new OA_UserRoleRelation() { RoleId = int.Parse(roleId), UserId = userId, InsertTime = DateTime.Now, UpdateTime = DateTime.Now };

                        num += _efHelper.Insert<OA_UserRoleRelation>(relation);

                    }

                }

                if (IdArray.Length == num)
                    return true;
                else
                    return false;


            }
            catch (Exception)
            {

                throw;
            }
        }

        #endregion



        #region 给对应的角色分配相应的菜单操作权限
        /// <summary>
        /// 给对应的角色分配相应的菜单操作权限
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="RoleId"></param>
        /// <returns></returns>
        public bool InsertRoleMenuRelation(string Ids, int RoleId)
        {
            var num = 0;
            try
            {
                if (Ids == "" || Ids == null)
                    return false;
                var roleMenuRelationList = _efHelper.QueryList<OA_Authority>(a => a.RoleId == RoleId);
                if (roleMenuRelationList.Count != 0)
                    _efHelper.Delete<OA_Authority>(roleMenuRelationList);
                string[] MenuIdArray = Ids.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                if (MenuIdArray.Length > 0)
                {
                    foreach (var item in MenuIdArray)
                    {
                        num += _efHelper.Insert<OA_Authority>(new OA_Authority() { RoleId = RoleId, MenuId = item, InserTime = DateTime.Now, UpdateTime = DateTime.Now });
                    }
                }
                if (MenuIdArray.Length == num)
                    return true;
                else
                    return false;

            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
    }
}
